//
//  FPHelpCenterController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/4/18.
//

#import "FPHelpCenterController.h"
#import "FPHelpCenterPopupView.h"
#import "FPCustomPopupView.h"
#import "FPCustomerModel.h"
#import "FPFeedBackController.h"

@interface FPHelpCenterController () <TTTAttributedLabelDelegate>

@property (weak, nonatomic) IBOutlet UILabel *tipLab1;
@property (weak, nonatomic) IBOutlet UILabel *tipLab2;
@property (weak, nonatomic) IBOutlet UIView *view1;
@property (weak, nonatomic) IBOutlet UIView *view2;
@property (weak, nonatomic) IBOutlet UIView *view3;
@property (weak, nonatomic) IBOutlet UILabel *lab1;
@property (weak, nonatomic) IBOutlet UILabel *lab2;
@property (weak, nonatomic) IBOutlet UILabel *lab3;
@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet UIView *backView;

@property (nonatomic , strong) FPHelpCenterPopupView *popupView;
@property (nonatomic , strong) NSMutableArray *contactSource;

@end

@implementation FPHelpCenterController

LazyLoadDataSource(contactSource)

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)initUI {
    
    self.tipLab1.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.tipLab2.textColor = [UIColor hexColor:@"#9A9A9A"];
    
    self.lab1.textColor = [UIColor hexColor:@"#6A6A6A"];
    self.lab2.textColor = [UIColor hexColor:@"#6A6A6A"];
    self.lab3.textColor = [UIColor hexColor:@"#6A6A6A"];
    
    self.view1.clipsToBounds = YES;
    self.view1.layer.cornerRadius = 4.f;
    self.view2.clipsToBounds = YES;
    self.view2.layer.cornerRadius = 4.f;
    self.view3.clipsToBounds = YES;
    self.view3.layer.cornerRadius = 4.f;
    
    self.btn1.clipsToBounds = YES;
    self.btn1.layer.cornerRadius = 8.f;
    self.btn1.layer.borderWidth = 1.f;
    self.btn1.layer.borderColor = [UIColor hexColor:@"#3A3A3A"].CGColor;
    [self.btn1 setTitleColor:[UIColor hexColor:@"#3A3A3A"] forState:(UIControlStateNormal)];
    
    self.btn2.clipsToBounds = YES;
    self.btn2.layer.cornerRadius = 8.f;
    [self.btn2 setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
    self.btn2.backgroundColor = MAIN_BLUE_COLOR;
    
    self.view.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    [self setTitle:@"帮助中心" style:(XYCustomNavigationStyleClear) hiddenBackBtn:NO];
    
    self.backView.backgroundColor = UIColor.clearColor;
    
    [self.view1 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(feedbackAction)]];
    
    [self getContactData];
}


- (void)getContactData {
    
    [MBManager showLoading];
    [FPCustomerModel getCustomersSuccess:^(NSInteger code, NSString * _Nonnull message, NSArray * _Nonnull list) {
        [MBManager hideLoading];
        
        [self.contactSource removeAllObjects];
        for (FPCustomerModel *model in list) {
            
            [self.contactSource addObject:model];
        }
        
        [self.view2 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(complainAction)]];
        [self.view3 addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cooperationAction)]];
        
    } failure:^(NSError * _Nonnull error) {
        [self showError:error];
    }];
}


- (FPHelpCenterPopupView *)popupView {
    
    if (!_popupView) {
        
        _popupView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPHelpCenterPopupView.class) owner:nil options:nil].lastObject;
        _popupView.contentLab.delegate = self;
    }
    return _popupView;
}

- (void)feedbackAction {
    
    FPFeedBackController *vc = [[FPFeedBackController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)complainAction {
    
    // xkd@fmshuke.com
}

- (void)cooperationAction {
    
    // 创建弹窗，指定高度为 400
    CGFloat popupHeight = 254.f / 375.f * SCREEN_WIDTH;  // 根据需求调整高度
    FPCustomPopupView *popupView = [[FPCustomPopupView alloc] initWithHeight:popupHeight];
    
    [popupView.contentView addSubview:self.popupView];
    self.popupView.frame = CGRectMake(0.f, 0.f, SCREEN_WIDTH, popupHeight);
    
    NSString *text = @"kfz@fmshuke.com";
    
    for (FPCustomerModel *model in self.contactSource) {
        
        if ([model.ID isEqualToString:@"2"]) {
            
            text = model.contactValue;
        }
    }
    
    NSRange range = [text rangeOfString:text];
    
    NSString *finalText = [NSString stringWithFormat:@"您可通过发送邮件到[%@]，邮件需包含您的商务合作方案、您的姓名、反馈电话等关键信息，您的邮件有效提交后，本平台会在15个工作日内安排相应工作人员与您取得联系，请您耐心等待，不要重复提交。", text];
    
    [self.popupView.contentLab addLinkToURL:[NSURL URLWithString:@"cooperation"] withRange:range];
    
    
    
    self.popupView.contentLab.text = finalText;
    
    // 显示弹窗
    [popupView showInView:self.view];
}

#pragma mark - TTTAttributedLabelDelegate 处理协议点击
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
 
    if ([url.absoluteString isEqualToString:@"cooperation"]) {
        
        [self showText:@"fsdlfjhalh"];
    }
}


@end
